package com.example.easybuy.controller;

import com.example.easybuy.annotaion.CheckPermission;
import com.example.easybuy.service.TypeService;
import com.example.easybuy.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @RequestMapping("/findSon")
    public AjaxResult findSon(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("hasSon",typeService.findSon(map));
    }

    @RequestMapping("/getList")
    public AjaxResult getList(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("typeList", typeService.getList(map));
    }

    @RequestMapping("/getParent")
    public AjaxResult getParent(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("myParent",typeService.getParent(map));
    }
    @RequestMapping("/selectMySon")
    public AjaxResult selectMySon(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("pageInfo",typeService.selectMySon(map));
    }

    @RequestMapping("/addType")
    @CheckPermission({"1"})
    public AjaxResult addType(@RequestParam Map<String,Object> map){
        if (map.get("type").toString().equals("1")){
            map.put("parentId",0);
        }
        return AjaxResult.success().add("isAdd",typeService.doAdd(map));
    }
    @RequestMapping("/delType")
    @CheckPermission({"1"})
    public AjaxResult delType(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("isDel",typeService.doDel(map));
    }
    @RequestMapping("/updateType")
    @CheckPermission({"1"})
    public AjaxResult updateType(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("isUpd",typeService.doUpdate(map));
    }

    @RequestMapping("/getCategory")
    public AjaxResult getCate(@RequestParam Map<String,Object> map){
        return AjaxResult.success().add("category",typeService.getCategory(map));
    }

    @RequestMapping("/findProduct")
    public AjaxResult findProduct(@RequestParam Map<String,Object> map){
        if (typeService.findProduct(map)){
            return AjaxResult.success().add("isFind",true);
        }
        return AjaxResult.error().add("isFind",false);
    }
}